package ncit.bluetooth.control;

import ncit.bluetooth.connect.BluetoothChatService;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class AccListener implements SensorEventListener {

	private static String TAG = "bluetooth.control.AccListener";

	private float x_s = 0, y_s = 0, z_s = 0;
	private float x, y, z;
	private float x_m, y_m, z_m;
	private float vx[] = new float[10];
	private float vy[] = new float[10];
	private float vz[] = new float[10];

	private boolean _start = false;
	private boolean _stop = false;
	private boolean _breaked = false;

	private BluetoothChatService _mChatService = null;
	private Handler _workingThreadHandler = null;

	public AccListener() {
	}

	public AccListener(Handler workingThreadHandler) {
		this._workingThreadHandler = workingThreadHandler;
		this._mChatService = BluetoothChatService.getInstance();
	}

	public AccListener(WorkingThread workingThread) {
		this._workingThreadHandler = workingThread.getHandler();
		this._mChatService = BluetoothChatService.getInstance();
	}

	public void onAccuracyChanged(Sensor arg0, int arg1) {

		x_s = x;
		y_s = y;
		z_s = z;
	}

	int i = 0;
	public void onSensorChanged(SensorEvent event) {
		
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

			// we calculate scaled values between -1 and 1
			float x_send = 0, y_send = 0;

			// assign directions
			x = event.values[0];
			y = event.values[1];
			z = event.values[2];

			if (i == 6) {

				for (int j = 0; j < 6; j++) {

					x_m = x_m + vx[j];
					y_m = y_m + vy[j];
					z_m = z_m + vz[j];

				}

				x_m = x_m / 6;
				y_m = y_m / 6;

				if (x_m < 0) {

					x_send = Math.abs(x_m) + x_s;
					if (x_send > 5)
						x_send = 1;
					else
						x_send = x_send / 5;

				} else {
					if (x_m < x_s) {

						x_send = x_s - x_m;
						if (x_send < 5)
							x_send = x_send / 5;
						else
							x_send = 1;

					}
					if (x_m > x_s) {

						x_send = x_m - x_s;
						if (x_send < 3)
							x_send = (-1) * (x_send / 3);
						else
							x_send = -1;

					}
				}

				if (y_m > y_s) {

					y_send = y_m - y_s;
					if (y_send > 5)
						y_send = 1;
					else
						y_send = y_send / 5;

				} else {

					y_send = y_s - y_m;
					if (y_send > 5)
						y_send = -1;
					else
						y_send = (-1) * y_send / 5;

				}

				i = 0;
				if (_mChatService != null && _workingThreadHandler != null) {
					
					if (!_stop) {// if braked it will not send accelerometer
									// data

						// here we send a message to the WorkingThread class
						// that will decide on our output
						Message msg = _workingThreadHandler
								.obtainMessage(WorkingThread.MESSAGE_FORWARD);
						msg.obj = -x_send;
						msg.sendToTarget();

						if (BluetoothControlActivity.DEBUG)
							Log.i(TAG, "The forward/backward slope is: "
									+ new Float(-x_send).toString());

						msg = _workingThreadHandler
								.obtainMessage(WorkingThread.MESSAGE_LEFT);
						msg.obj = y_send;
						msg.sendToTarget();
						_breaked = false;

						if (BluetoothControlActivity.DEBUG)
							Log.i(TAG, "The right/left slope is: " + y_send);

					}
				}
			}

			vx[i] = x;
			vy[i] = y;
			vz[i] = z;
			i++;
		}
	}

	public void start() {

		x_s = x;
		y_s = y;
		z_s = z;
		_start = true;
		_stop = false;

	}

	public void stop() {

		_start = false;
		_stop = true;

	}

	public void setWorkingThreadHandler(Handler workingThreadHandler) {
		this._workingThreadHandler = workingThreadHandler;
	}

	public Handler getWorkingThreadHandler() {
		return this._workingThreadHandler;
	}

	public BluetoothChatService getChatService() {
		return _mChatService;
	}

	public void setChatService(BluetoothChatService _mChatService) {
		this._mChatService = _mChatService;
	}

}
